package sz.szsmk.citizencard.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.chenenyu.router.Router;

import javax.inject.Inject;

import dagger.BindsInstance;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.CheckSmsReponse;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.ISignInMvpView;
import sz.szsmk.citizencard.mvp.LoginByMessageMvpView;
import sz.szsmk.citizencard.mvp.presenter.LoginByMessagePresenter;
import sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity;
import sz.szsmk.citizencard.util.ACache;

/**
 * author : hyt
 * date : 2019/8/29
 * description :
 */
public class LoginByMessageActivity extends AppBaseActivity implements LoginByMessageMvpView , View.OnClickListener {

    @Inject
    LoginByMessagePresenter presenter;

    CountDownTimerUtil countDownTimerUtil;

    Button btn_sendMessage;

    EditText et_phoneNumber;

    EditText et_verificationCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.login_bymessage_activity);


        btn_sendMessage = findViewById(R.id.btn_sendMessage);
        btn_sendMessage.setOnClickListener(this);
        et_phoneNumber = findViewById(R.id.et_phoneNumber);

        et_verificationCode = findViewById(R.id.et_verificationCode);

        findViewById(R.id.login_btn).setOnClickListener(this);
        findViewById(R.id.back).setOnClickListener(this);



        countDownTimerUtil = new CountDownTimerUtil(120000, 1000,btn_sendMessage);
    }

    public void onClick1(View v){
        int id = v.getId();
        if (id == R.id.btn_sendMessage) {
            if (!TextUtils.isEmpty(et_phoneNumber.getText().toString())) {
                btn_sendMessage.setEnabled(false);
                countDownTimerUtil.start();
                presenter.sendMessage(et_phoneNumber.getText().toString());
            } else {
                Toast.makeText(LoginByMessageActivity.this, "请输入手机号", Toast.LENGTH_LONG).show();
            }
        } else if (id == R.id.login_btn) {
            if (!TextUtils.isEmpty(et_verificationCode.getText().toString().trim())) {
                presenter.checkCheckSmsCode(et_phoneNumber.getText().toString(), et_verificationCode.getText().toString());
            } else {
                Toast.makeText(LoginByMessageActivity.this, "请输入验证码", Toast.LENGTH_LONG).show();
            }
        } else if (id == R.id.back) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        countDownTimerUtil.onFinish();
        countDownTimerUtil = null;
    }
    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{ presenter };
    }
    @Override
    protected CharSequence getTopTitle() {
        return null;
    }

    @Override
    public void sendMessage(BaseBean bean) {
        //发送短信返回
        if("000000".equals(bean.getCode())){
            Toast.makeText(this,"验证码发送成功",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void checkSmsCodeResult(CheckSmsReponse bean) {
        //验证码校验
        //1注册用户 2非注册用户
        if("1".equals(bean.getIsRegister())){
            ACache.get(DaggerApplication.getIns()).put("access_token",bean.getAccess_token());
            ACache.get(DaggerApplication.getIns()).put("refresh_token",bean.getRefresh_token());
            Router.build("main").go(this);
            finish();
        }else if ("2".equals(bean.getIsRegister())){
            Intent intent = new Intent(this,RegistBySmsActivity.class);
            intent.putExtra("mobile",et_phoneNumber.getText().toString().trim());
            intent.putExtra("sms_code",et_verificationCode.getText().toString().trim());
            startActivity(intent);
        }
    }

    @Override
    public void onClick(View v) {
        onClick1(v);
    }

    class CountDownTimerUtil extends CountDownTimer{

        Button btn_sendMessage;

        public CountDownTimerUtil(long millisInFuture, long countDownInterval,Button btn_sendMessage) {
            super(millisInFuture, countDownInterval);
            this.btn_sendMessage = btn_sendMessage;
        }

        @Override
        public void onTick(long millisUntilFinished) {
                btn_sendMessage.setText("重新发送:" + millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            btn_sendMessage.setText("获取验证码");
            btn_sendMessage.setEnabled(true);
        }
    }

}
